package net.kem.android.positioner;

import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.content.SharedPreferences;

class Settings {
	public static final String PREFS_NAME = "net.kem.android.positioner.Settings";
	public static final String PREFS_POI_NAME = "net.kem.android.positioner.Settings_POI";
	private static SharedPreferences prefs;
	private static final String PARAM_HOST = "host";
	private static final String PARAM_PORT = "port";
	private static final String PARAM_USERNAME = "username";
	private static final String PARAM_PASSWORD = "password";
	private static final String PARAM_GPS_AUTO_REFRESH_DISTANCE = "GPSAutoRefreshDistance";
	private static final String PARAM_GPS_REFRESH_DISTANCE = "GPSRefreshDistance";
	private static final String PARAM_GPS_REFRESH_INTERVAL = "GPSRefreshInterval";
	private static final String PARAM_POI_RADIUS = "PoiRadius";

	public static final String PARAM_IS_SERVICE_RUNNING = "IsServiceRunning";

	public static final int GPS_REFRESH_DISTANCE_DEFAULT = 100;
	private static String host;
	private static int port;
	private static String username;
	private static String password;
	private static int gpsRefreshInterval;
	private static int gpsRefreshDistance;
	private static boolean gpsAutoRefreshDistance;
	private static int poiRadius;

	private static final Map<String, POI> POIS = new TreeMap<String, POI>();

	static void init(Context activity) {
		if(prefs == null) {
			prefs = activity.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
			if(getHost().length() == 0) {
				setHost("znetwork.no-ip.org");
			}
			if(getUsername().length() == 0) {
				setUsername("0507362270");
				setGPSAutoRefreshDistance(true);
			}
			if(getPassword().length() == 0) {
				setPassword("p1r0zH0k@tEnE");
			}
			boolean onePOIfound = false;
			for(Map.Entry<String, ?> me: prefs.getAll().entrySet()) {
				if(me.getKey().startsWith("POI_")) {
					String name = me.getKey().substring("POI_".length());
					POIS.put(name, new POI(name, (String)me.getValue()));
					onePOIfound = true;
				}
			}
			if(!onePOIfound) {
				POIS.put("Home", new POI("Home").store());
				POIS.put("Work", new POI("Work").store());
				POIS.put("Supermartket", new POI("Supermartket").store()); //Club
				POIS.put("Bank", new POI("Bank").store()); //Gym
				POIS.put("Friends", new POI("Friends").store()); //Pub
			}
		}
	}

	static String getValue(String key) {
		String res = prefs.getString(key, "");
		return res;
	}
	static void setValue(String key, String value) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(key, value);
		editor.commit();
	}

	static Boolean getValueBoolean(String key) {
		Boolean res = prefs.getBoolean(key, false);
		return res;
	}
	static void setValue(String key, Boolean value) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	static Float getValueFloat(String key) {
		Float res = prefs.getFloat(key, -1.0f);
		return res;
	}
	static void setValue(String key, Float value) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	static Integer getValueInt(String key, int defValue) {
		Integer res = prefs.getInt(key, defValue);
		return res;
	}
	static void setValue(String key, Integer value) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(key, value);
		editor.commit();
	}


	static String getHost() {
		if(host == null || host.length() == 0) {
			host = prefs.getString(PARAM_HOST, "");
		}
		return host;
	}
	static void setHost(String host) {
		Settings.host = host;
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(PARAM_HOST, host);
		editor.commit();
	}
	static int getPort() {
		if(port == 0) {
			port = getValueInt(PARAM_PORT, 80);
		}
		return port;
	}
	static void setPort(int port) {
		Settings.port = port;
		setValue(PARAM_PORT, port);
	}
	static String getUsername() {
		if(username == null || username.length() == 0) {
			username = prefs.getString(PARAM_USERNAME, "");
		}
		return username;
	}
	static void setUsername(String username) {
		Settings.username = username;
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(PARAM_USERNAME, username);
		editor.commit();
	}
	static String getPassword() {
		if(password == null || password.length() == 0) {
			password = prefs.getString(PARAM_PASSWORD, "");
		}
		return password;
	}
	static void setPassword(String password) {
		Settings.password = password;
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(PARAM_PASSWORD, password);
		editor.commit();
	}

	static int getGPSRefreshDistance() {
		if(gpsRefreshDistance == 0) {
			gpsRefreshDistance = getValueInt(PARAM_GPS_REFRESH_DISTANCE, GPS_REFRESH_DISTANCE_DEFAULT);
		}
		return gpsRefreshDistance;
	}
	static void setGPSRefreshDistance(int newGpsRefreshDistance) {
		gpsRefreshDistance = newGpsRefreshDistance;
		setValue(PARAM_GPS_REFRESH_DISTANCE, gpsRefreshDistance);
	}

	static boolean getGPSAutoRefreshDistance() {
		gpsAutoRefreshDistance = getValueBoolean(PARAM_GPS_AUTO_REFRESH_DISTANCE);
		return gpsAutoRefreshDistance;
	}
	static void setGPSAutoRefreshDistance(boolean gpsAutoRefreshDistance) {
		Settings.gpsAutoRefreshDistance = gpsAutoRefreshDistance;
		setValue(PARAM_GPS_AUTO_REFRESH_DISTANCE, gpsAutoRefreshDistance);
	}

	static int getGPSRefreshInterval() {
		if(gpsRefreshInterval == 0) {
			gpsRefreshInterval = getValueInt(PARAM_GPS_REFRESH_INTERVAL, 0);
		}
		return gpsRefreshInterval;
	}
	static void setGPSRefreshInterval(int gpsRefreshInterval) {
		Settings.gpsRefreshInterval = gpsRefreshInterval;
		setValue(PARAM_GPS_REFRESH_INTERVAL, gpsRefreshInterval);
	}


	// POI Begin
	static Map<String, POI> getPOIs() {
		return POIS;
	}

	static boolean isPOIDefined(String poiName) {
		boolean res;
		POI poi = POIS.get(poiName);
		if(poi != null) {
			res = poi.isDefined();
		} else {
			res = false;
		}
		return res;
	}

	static POI getPOIByName(String poiName) {
		return POIS.get(poiName);
	}

	static POI getById(int rId) {
		POI res;
		switch(rId) {
			case R.id.contextMenuHome:
				res = POIS.get("Home");
				break;
			case R.id.contextMenuWork:
				res = POIS.get("Work");
				break;
			case R.id.contextMenuSupermartket:
				res = POIS.get("Supermartket");
				break;
			case R.id.contextMenuBank:
				res = POIS.get("Bank");
				break;
			case R.id.contextMenuFriends:
				res = POIS.get("Friends");
				break;
			default: res = null;
		}
		return res;
	}
	// POI End

	static int getPOIRadius() {
		if(poiRadius == 0) {
			poiRadius = getValueInt(PARAM_POI_RADIUS, 40);
		}
		return poiRadius;
	}

	static void setPOIRadius(int poiRadius) {
		Settings.poiRadius = poiRadius;
		setValue(PARAM_POI_RADIUS, poiRadius);
	}
}